package com.snowfeel.sfmall.sys.controller;

import com.snowfeel.sfmall.core.enums.ResponseCode;
import com.snowfeel.sfmall.core.response.JsonResult;
import com.snowfeel.sfmall.core.vo.PageVo;
import com.snowfeel.sfmall.security.constants.SecurityConstants;
import com.snowfeel.sfmall.sys.dto.LoginParam;
import com.snowfeel.sfmall.sys.dto.SysUserParam;
import com.snowfeel.sfmall.sys.dto.SysUserRoleParam;
import com.snowfeel.sfmall.sys.query.SysUserQuery;
import com.snowfeel.sfmall.sys.service.SysUserService;
import com.snowfeel.sfmall.sys.vo.LoginUserVo;
import com.snowfeel.sfmall.sys.vo.SysRoleVo;
import com.snowfeel.sfmall.sys.vo.SysUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台用户管理
 * Created on 2018/4/26.
 */
@RestController
@Api(tags = "SysUserController", description = "后台用户管理")
@RequestMapping("/user")
public class SysUserController {

    @Autowired
    private SysUserService userService;

    @ApiOperation(value = "用户注册")
    @PostMapping("/register")
    public JsonResult<SysUserVo> register(@RequestBody SysUserParam userParam) {
        SysUserVo userVo = userService.register(userParam);
        if (userVo == null) {
            JsonResult.error(ResponseCode.FAIL);
        }
        return JsonResult.success(userVo);
    }

    @ApiOperation(value = "登录以后返回token")
    @PostMapping("/login")
    public JsonResult login(@RequestBody LoginParam umsAdminLoginParam) {
        String token = userService.login(umsAdminLoginParam.getUsername(), umsAdminLoginParam.getPassword());
        if (token == null) {
            return JsonResult.error("用户名或密码错误");
        }
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        return JsonResult.success(tokenMap);
    }

    @ApiOperation(value = "刷新token")
    @GetMapping("/refreshToken")
    public JsonResult refreshToken(HttpServletRequest request) {
        String token = request.getHeader(SecurityConstants.AUTHORIZATION);
        String refreshToken = userService.refreshToken(token);
        if (refreshToken == null) {
            return JsonResult.error("token已经过期！");
        }
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", refreshToken);
        return JsonResult.success(tokenMap);
    }

    @ApiOperation(value = "获取当前登录用户信息")
    @PostMapping("/info")
    public JsonResult<LoginUserVo> getUserInfo(@RequestParam String username) {
        LoginUserVo loginUserVo = userService.getUserInfo(username);
        return JsonResult.success(loginUserVo);
    }

    @ApiOperation(value = "登出功能")
    @PostMapping("/logout")
    public JsonResult logout() {
        return JsonResult.success(null);
    }

    @ApiOperation("根据用户名或姓名分页获取用户列表")
    @PostMapping("/page")
    public JsonResult<PageVo<SysUserVo>> page(@RequestBody SysUserQuery query) {
        PageVo<SysUserVo> page = userService.page(query);
        return JsonResult.success(page);
    }

    @ApiOperation("获取指定用户信息")
    @GetMapping( "/{id}")
    public JsonResult<SysUserVo> getItem(@PathVariable Long id) {
        SysUserVo userVo = userService.getItem(id);
        return JsonResult.success(userVo);
    }

    @ApiOperation("修改指定用户信息")
    @PostMapping("/update/{id}")
    public JsonResult update(@PathVariable Long id, @RequestBody SysUserParam userParam) {
        int count = userService.update(id, userParam);
        if (count > 0) {
            return JsonResult.success(count);
        }
        return JsonResult.error(ResponseCode.FAIL);
    }

    @ApiOperation("删除指定用户信息")
    @PostMapping("/delete/{id}")
    public JsonResult delete(@PathVariable Long id) {
        int count = userService.delete(id);
        if (count > 0) {
            return JsonResult.success(count);
        }
        return JsonResult.error(ResponseCode.FAIL);
    }

    @ApiOperation("给用户分配角色")
    @PostMapping("/role/update")
    public JsonResult updateRole(@RequestBody SysUserRoleParam userRoleParam) {
        int count = userService.updateRole(userRoleParam);
        if (count >= 0) {
            return JsonResult.success(count);
        }
        return JsonResult.error(ResponseCode.FAIL);
    }

    @ApiOperation("获取指定用户的角色")
    @GetMapping("/role/{userId}")
    public JsonResult<List<SysRoleVo>> getRoleList(@PathVariable Long userId) {
        List<SysRoleVo> roleList = userService.getRoleList(userId);
        return JsonResult.success(roleList);
    }

}
